//
//  UserAccount.swift
//  weibo
//
//  Created by youyongpeng on 15/11/24.
//  Copyright © 2015年 youyongpeng. All rights reserved.
//

import UIKit

class UserAccoint: NSObject, NSCoding {
    

    /// 用于调用access_token，接口获取授权后的access token。
    var access_token: String?
    /// access_token的生命周期，单位是秒数。
    var expires_in: NSTimeInterval = 0 {
        
        didSet {
            expiresData = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    /// 保存过期时间
    var expiresData: NSDate?
    
    /// 当前授权用户的UID。
    var uid: String?
    /// 用户昵称
    var screen_name: String?
    /// 用户头像地址（大图），180×180像素
    var avatar_large: String?

    init(dict: [String: AnyObject]) {

        super.init()
    
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
    
        let keys = ["access_token", "expires_in", "uid", "expiresData", "screen_name", "avatar_large"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
    
    
    /// MARK: - 编码
    /// 归档: 把对象对象保存到磁盘以前, 把对象编码转换成二进制数据, 在进行保存, 跟网络的序列化很像
    ///
    /// - parameter aCoder: 编码器
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expiresData, forKey: "expiresData")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    
    /// MARK: - 解码
    
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expiresData = aDecoder.decodeObjectForKey("expiresData") as? NSDate
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }

}